package nio.channel;

import com.sun.security.ntlm.Server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class SocketChannelDemo {

    public static void main(String[] args) throws IOException {

        Pipe pipe = Pipe.open();
        Pipe.SinkChannel sink = pipe.sink();
        Pipe.SourceChannel source = pipe.source();

        String newData = "New String to write to file..." + System.currentTimeMillis();

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.clear();

        buffer.put(newData.getBytes());

        buffer.flip();

        while (buffer.hasRemaining()) {

            sink.write(buffer);
        }



        buffer.clear();
        int read = source.read(buffer);


        while (read != -1) {


            while (buffer.hasRemaining()) {
                buffer.flip();
                System.out.print(buffer.get());

            }


            buffer.compact();
            read = source.read(buffer);




        }


    }
}
